package me.youline.dataServer.service.image;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import me.youline.dataServer.exception.ImageScaleException;

import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.simpleimage.ImageFormat;
import com.alibaba.simpleimage.ImageRender;
import com.alibaba.simpleimage.ImageWrapper;
import com.alibaba.simpleimage.SimpleImageException;
import com.alibaba.simpleimage.render.ReadRender;
import com.alibaba.simpleimage.render.ScaleParameter;
import com.alibaba.simpleimage.render.WriteParameter;
import com.alibaba.simpleimage.util.ImageScaleHelper;
import com.alibaba.simpleimage.util.ImageWriteHelper;

@Component
public class DefaultImageScaleService implements ImageScaleService {
	
	private static final Logger logger = LoggerFactory.getLogger(DefaultImageScaleService.class);

	
	@Override
	@Profiled(tag = "service- 图片略缩时间")
	public List<File> scaleImage(ImageScaleContext context)
			throws ImageScaleException {

		List<File> list = new ArrayList<File>();
		FileInputStream sourceInputStream = null;
		try {
			sourceInputStream = new FileInputStream(context.getSourceFile());
			ImageRender r = new ReadRender(sourceInputStream);
			ImageWrapper w = r.render();
			for (ImageSize size : context.getScaleSizes()) {
				try {
					if(size.isBlankSize()){
						continue;
					}
					ImageWrapper res = null;
					if (w.getImageFormat() == ImageFormat.GIF) {
						res = ImageScaleHelper.scaleGIF(w, new ScaleParameter(
								size.getWidth(), size.getHeight()));
					} else {
						res = new ImageWrapper(ImageScaleHelper.scale(
								w.getAsPlanarImage(),
								new ScaleParameter(size.getWidth(), size
										.getHeight())));
					}
					res.setImageFormat(w.getImageFormat());
					File successWtitedFile = this.writeToFile(context, res,
							size);
					list.add(successWtitedFile);
				} catch (Exception e) {
					logger.info(e.getMessage(),e);
				}

			}
		} catch (Exception e) {
			throw new ImageScaleException(e.getMessage(), e);
		} finally {
			if (sourceInputStream != null) {
				try {
					sourceInputStream.close();
				} catch (IOException e) {
				
				}
			}
		}
		return list;

	}

	/**
	 * 将略缩后的图片写入到指定文件
	 * @param context
	 * @param w
	 * @param size
	 * @return
	 * @throws SimpleImageException
	 * @throws FileNotFoundException
	 */
	protected File writeToFile(ImageScaleContext context, ImageWrapper w,
			ImageSize size) throws SimpleImageException, FileNotFoundException {
		FileOutputStream out = null;
		File file = context.getTargetFile(size);
		try {
			out = new FileOutputStream(file);
			ImageWriteHelper.write(w, out, w.getImageFormat(),
					new WriteParameter());
			return file;
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					// 异常丢弃
				}
			}
		}
	}
}
